// pages/withdraw/withdraw.js
var util = require('../../utils/util.js')
var http = require('../../utils/http.js')

const app = getApp();

// 在页面中定义插屏广告
let interstitialAd = null
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selected: false,
    numList: [
      {
        num: '20',
        choose: false,
      },
      {
        num: '50',
        choose: false,
      },
      {
        num: '100',
        choose: false,
      }
    ],
  },
  insertScreenAdTimer: '',
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var userMoney = app.globalData.money;
    that.setData({
      userMoney,
    })

    // 在页面onLoad回调事件中创建插屏广告实例
    if (wx.createInterstitialAd) {
      interstitialAd = wx.createInterstitialAd({
        adUnitId: 'adunit-7f5cc40bbeb7c980'
      })
      interstitialAd.onLoad(() => {})
      interstitialAd.onError((err) => {})
      interstitialAd.onClose(() => {})
    }
  },
  screenAD: function() {
    // 在适合的场景显示插屏广告
    if (interstitialAd) {
      interstitialAd.show().catch((err) => {
      console.error(err)
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    http.withdrawList().then( res => {
      that.setData({
        extractKinds: res.extractKinds
      })
    })
    var count = 0;
    var insertScreenAdTimer = setInterval(function () {
      count ++;
      console.log(count);
      if(count > 1) {
        that.screenAD();
        clearInterval(insertScreenAdTimer);
      }
    },1000)
    that.insertScreenAdTimer = insertScreenAdTimer;
  },
  
  back:function () {
    wx.navigateBack({
      delta: 1
    })
  },
  // selected: function() {
  //   var that = this;
  //   var selected = that.data.selected === false? true:false;
  //   that.setData({
  //     selected,
  //   })
  // },
  choose:function(e) {
    var that = this;
    var numList = that.data.numList;
    var index = e.currentTarget.dataset.index;
    for(var i = 0; i < numList.length; i++) {
      if(i === index) {
        numList[i].choose = numList[i].choose === false? true:false;
        that.setData({
          numList,
          index,
        })
      } else {
        numList[i].choose = false;
        that.setData({
          numList,
          index
        })
      }
    }
  },
  //提现
  withdraw: function() {
    var that = this;
    var index = that.data.index;
    console.log(index);
    var numList = that.data.numList;
    var money = numList[index].num * 10000;
    const data = {
      money: money,
    }
    http.withdraw(data).then( res => {

    }, err => {
      console.log(err)
      wx.showModal({
        title: '提示',
        content: err,
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})